package System

import (
	"fmt"
	"net/http"
	"source-admin-go/pkg/e"
	"source-admin-go/src/config"
	"source-admin-go/src/config/Redis"
	"source-admin-go/src/config/Routes"
	"source-admin-go/src/models"
)

const (
	ConstSystemName = "Source-Admin-Go"
)

func SysGlobalStartInit() {
	center := fmt.Sprintf("System ready！Welcome to use %v system", ConstSystemName)
	e.SuccessLogPrintln(center)
}

func SysRouterInit() {
	routersInit := Routes.InitRouter()

	server := config.Server
	var url string
	var port uint
	switch server.Env {
	case "PRODUCTION":
		url = server.Service.Production.Url
		port = server.Service.Production.Port
		break
	case "DEV":
		url = server.Service.Dev.Url
		port = server.Service.Dev.Port
		break
	default:
		if server.Service.Localhost.Url != "" {
			url = server.Service.Localhost.Url
		} else {
			url = "localhost"
		}
		if server.Service.Localhost.Port != 0 {
			port = server.Service.Localhost.Port
		} else {
			port = 8080
		}

	}

	addr := fmt.Sprintf("%v:%v", url, port)

	system := &http.Server{
		Addr:    addr,
		Handler: routersInit,
	}

	path := fmt.Sprintf("http://%v", addr)
	e.SuccessLogPrintln(path)
	_ = system.ListenAndServe()

}

func TableInit() {
	models.BaseModel{}.InitTable()
}

func RedisInit() {
	Redis.ServiceValidation()
}
